package com.zyf.service;

import cn.hutool.core.io.FileUtil;
import com.zyf.utils.StringUtil;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 处理字幕文件类
 */
public class HandleSrtService {


    /**
     * 批量处理成竖排
     */
    public void batchHandleSrtByVertical(String folderPath) throws Exception {
        List<File> srtList = FileUtil.loopFiles(folderPath, backfile -> backfile.isFile() && backfile.getName().endsWith(".srt"));
        //遍历文件夹批量处理
        for (File file : srtList) {
            BufferedReader reader = null;
            String fielrPath = file.getAbsolutePath();
            String temp = "";
            StringBuffer buffer = new StringBuffer();
            StringBuffer titleBuffer = new StringBuffer();
            InputStream inputstream = null;
            try {
                String fileName=file.getName();
                if(!fileName.contains("_")){
                    throw new RuntimeException("请维护srt文件中标题的结束行");
                }
                int endLine=Integer.valueOf(fileName.substring(fileName.indexOf("_")+1,fileName.indexOf(".")));
                inputstream = new FileInputStream(new File(fielrPath));
                reader = new BufferedReader(new InputStreamReader(inputstream));
                int index=1;
                int currentLine= 0;
                while ((temp = reader.readLine()) != null) {
                    //判断第一个字符是否为数字，如果是数字则整行添加；如果不是数字，则换行操作
                    if(!StringUtil.isBlank(temp)&&!"\uFEFF".equals(temp)){
                        temp=temp.trim();
                        try {
                            currentLine = Integer.valueOf(temp);
                        } catch (NumberFormatException e) {
                            System.out.println("不是数字行");
                        }
                        if(currentLine<=endLine){
                            titleBuffer.append(temp);
                            titleBuffer.append("\n");
                        }else{
                            char[] charStrs=temp.toCharArray();
                            if(isNumeric(charStrs[0])){
                                if(temp.length()<4){
                                        buffer.append(index+"");
                                        buffer.append("\n");
                                        index++;
                                }else{
                                    buffer.append(temp);
                                    buffer.append("\n");
                                }
                            }else{
                                temp=temp.replace(" ","&");
                                for(char str:temp.toCharArray()){
                                    buffer.append(str);
                                    buffer.append("\n");
                                }
                                buffer.append("\n");
                            }
                        }
                    }
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (inputstream != null) {
                    inputstream.close();
                }
            }

            //写入文件
            FileWriter writer = null;
            try {
                String finalsrt = fielrPath.replace(".srt", "content.srt");
                writer = new FileWriter(finalsrt, false);
                writer.write(buffer.toString());
            } catch (IOException e) {
                throw new RuntimeException(e);
            } finally {
                if (writer != null) {
                    writer.flush();
                    writer.close();
                }
            }
            //写入标题文件
            try {
                String finalsrt = fielrPath.replace(".srt", "title.srt");
                writer = new FileWriter(finalsrt, false);
                writer.write(titleBuffer.toString());
            } catch (IOException e) {
                throw new RuntimeException(e);
            } finally {
                if (writer != null) {
                    writer.flush();
                    writer.close();
                }
            }
        }
    }

    /**
     * 判断字符是否为数字
     * @param charStr
     * @return
     */
    private boolean isNumeric(char charStr){
        if(Character.isDigit(charStr)){
            return true;
        }else{
            return false;
        }
    }


}
